{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n# Customized Colorbars Tutorial\n\n\nThis tutorial shows how to build colorbars without an attached plot.\n\nCustomized Colorbars\n====================\n\n`~matplotlib.colorbar.ColorbarBase` puts a colorbar in a specified axes,\nand can make a colorbar for a given colormap; it does not need a mappable\nobject like an image. In this tutorial we will explore what can be done with\nstandalone colorbar.\n\nBasic continuous colorbar\n-------------------------\n\nSet the colormap and norm to correspond to the data for which the colorbar\nwill be used. Then create the colorbar by calling\n:class:`~matplotlib.colorbar.ColorbarBase` and specify axis, colormap, norm\nand orientation as parameters. Here we create a basic continuous colorbar\nwith ticks and labels. For more information see the\n:mod:`~matplotlib.colorbar` API.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\nimport matplotlib as mpl\n\nfig, ax = plt.subplots(figsize=(6, 1))\nfig.subplots_adjust(bottom=0.5)\n\ncmap = mpl.cm.cool\nnorm = mpl.colors.Normalize(vmin=5, vmax=10)\n\ncb1 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,\n                                norm=norm,\n                                orientation='horizontal')\ncb1.set_label('Some Units')\nfig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Discrete intervals colorbar\n---------------------------\n\nThe second example illustrates the use of a\n:class:`~matplotlib.colors.ListedColormap` which generates a colormap from a\nset of listed colors, :func:`colors.BoundaryNorm` which generates a colormap\nindex based on discrete intervals and extended ends to show the \"over\" and\n\"under\" value colors. Over and under are used to display data outside of the\nnormalized [0,1] range. Here we pass colors as gray shades as a string\nencoding a float in the 0-1 range.\n\nIf a :class:`~matplotlib.colors.ListedColormap` is used, the length of the\nbounds array must be one greater than the length of the color list. The\nbounds must be monotonically increasing.\n\nThis time we pass some more arguments in addition to previous arguments to\n:class:`~matplotlib.colorbar.ColorbarBase`. For the out-of-range values to\ndisplay on the colorbar, we have to use the *extend* keyword argument. To use\n*extend*, you must specify two extra boundaries. Finally spacing argument\nensures that intervals are shown on colorbar proportionally.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(6, 1))\nfig.subplots_adjust(bottom=0.5)\n\ncmap = mpl.colors.ListedColormap(['red', 'green', 'blue', 'cyan'])\ncmap.set_over('0.25')\ncmap.set_under('0.75')\n\nbounds = [1, 2, 4, 7, 8]\nnorm = mpl.colors.BoundaryNorm(bounds, cmap.N)\ncb2 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,\n                                norm=norm,\n                                boundaries=[0] + bounds + [13],\n                                extend='both',\n                                ticks=bounds,\n                                spacing='proportional',\n                                orientation='horizontal')\ncb2.set_label('Discrete intervals, some other units')\nfig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Colorbar with custom extension lengths\n--------------------------------------\n\nHere we illustrate the use of custom length colorbar extensions, used on a\ncolorbar with discrete intervals. To make the length of each extension the\nsame as the length of the interior colors, use ``extendfrac='auto'``.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(6, 1))\nfig.subplots_adjust(bottom=0.5)\n\ncmap = mpl.colors.ListedColormap(['royalblue', 'cyan',\n                                  'yellow', 'orange'])\ncmap.set_over('red')\ncmap.set_under('blue')\n\nbounds = [-1.0, -0.5, 0.0, 0.5, 1.0]\nnorm = mpl.colors.BoundaryNorm(bounds, cmap.N)\ncb3 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,\n                                norm=norm,\n                                boundaries=[-10] + bounds + [10],\n                                extend='both',\n                                extendfrac='auto',\n                                ticks=bounds,\n                                spacing='uniform',\n                                orientation='horizontal')\ncb3.set_label('Custom extension lengths, some other units')\nfig.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
